﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Business
{
    public class HoaDonMua
    {

        public int MaHDNhap { get; set; }
        public string MaHDNhapHienThi
        {
            get
            {
                return "HDM" + MaHDNhap;
            }
        }
      
        
        public int MaDonDatNCC { get; set; }
        public string MaDonDatNCCHienThi
        {
            get
            {
                return "DDNCC" + MaDonDatNCC;
            }
        }
        public DateTime NgayMua { get; set; }
        public double TongGT { get; set; }
        public double TienSauThue { get; set; }

        public double ThueSuat { get; set; }

        public double ThueSuatHienThi
        {
            get
            {
                return ThueSuat * 100;
            }
        }
        public List<ChiTietHDMua> DanhSachChiTietHDMua { get; set; }

         public HoaDonMua()
        {
            this.MaHDNhap = 0;
            this.ThueSuat = 0.1;
            DanhSachChiTietHDMua = new List<ChiTietHDMua>();
        }

         public HoaDonMua(int maHDNhap,  int maDonDatNCC, DateTime ngayMua,
            double tongGT, double tienSauThue, double thueSuat)
        {
            this.MaHDNhap = maHDNhap;
            this.MaDonDatNCC = maDonDatNCC;
            this.NgayMua = ngayMua;
            this.TongGT= tongGT;
            this.TienSauThue = tienSauThue;
            this.ThueSuat = thueSuat;
        }


         public void ThemMoiChiTietHDMua(int maHDNhap, int maHang,
           string tenHang, double giaNhap, int soLuong, double chietKhau)
         {
             ChiTietHDMua chiTietHDM = new ChiTietHDMua(maHDNhap, maHang, tenHang, giaNhap, soLuong, chietKhau);
             DanhSachChiTietHDMua.Add(chiTietHDM);
         }

         public void CapNhatChiTietDonDat(int index, int maHang,
             string tenHang, double giaNhap, int soLuong, double chietKhau)
         {
             ChiTietHDMua chiTietDonDat = DanhSachChiTietHDMua[index];
             chiTietDonDat.CapNhat(maHang, tenHang, giaNhap, soLuong, chietKhau);
         }

         public void XoaChiTietHDMua(int index)
         {
             DanhSachChiTietHDMua.RemoveAt(index);
         }

         private bool TonTaiChiTietHDMua(int maCTHDN)
         {
             foreach (var chitietHDMua in this.DanhSachChiTietHDMua)
             {
                 if (chitietHDMua.MaCTHDN == maCTHDN)
                     return true;
             }
             return false;
         }

         private double TinhTongTienTruocThue()
         {
             double tongTien = 0;
             foreach (var chiTietHDM in DanhSachChiTietHDMua)
             {
                 double giaTriThuc = chiTietHDM.GiaNhap - chiTietHDM.GiaNhap * chiTietHDM.ChietKhau;
                 tongTien = tongTien + chiTietHDM.SoLuong * giaTriThuc;
             }
             this.TongGT = tongTien;
             return tongTien;
         }


         public static int ThemMoi(HoaDonMua nhapObject)
         {
             DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

             DataAccess.HoaDonNhap tamObject = new DataAccess.HoaDonNhap();
             tamObject.MaDonDatNCC = nhapObject.MaDonDatNCC;
             

             tamObject.NgayMua = nhapObject.NgayMua;

             tamObject.ThueSuat = nhapObject.ThueSuat;
             tamObject.TongGT = nhapObject.TongGT;

             tamObject.TienSauThue = nhapObject.TienSauThue;
             db.HoaDonNhaps.InsertOnSubmit(tamObject);

             db.SubmitChanges();

             foreach (var chitietHDMua in nhapObject.DanhSachChiTietHDMua)
             {
                 DataAccess.ChiTietHDNhap chiTietTamObject = new DataAccess.ChiTietHDNhap();
                 chiTietTamObject.MaHDNhap = tamObject.MaHDNhap;
                 chiTietTamObject.MaHang = chitietHDMua.MaHang;

                 chiTietTamObject.SoLuong = chitietHDMua.SoLuong;
                 chiTietTamObject.GiaNhap = chitietHDMua.GiaNhap;

                 chiTietTamObject.ChietKhau = chitietHDMua.ChietKhau;

                 db.ChiTietHDNhaps.InsertOnSubmit(chiTietTamObject);
             }

             db.SubmitChanges();

             return tamObject.MaHDNhap;
         }

         public double TinhTongTienSauThue(double thueSuatPhanTram)
         {
             this.ThueSuat = thueSuatPhanTram / 100;
             double tongGT = TinhTongTienTruocThue();
             double tienSauThue = tongGT + tongGT * this.ThueSuat;
             this.TienSauThue = tienSauThue;
             return tienSauThue;
         }


         public static bool ChoPhepXoaChinhSua(int maHDNhap)
         {
             DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

             int hoaDonNhap_PNK = (from p in db.PhieuNhapKhos
                                  where p.MaHDNhap == maHDNhap
                                  select p).Count();

             if (hoaDonNhap_PNK > 0)
                 return false;
             else
                 return true;
         }


         public static void CapNhat(HoaDonMua nhapObject)
         {
             DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();


             var tamObject = (from p in db.HoaDonNhaps
                              where p.MaHDNhap == nhapObject.MaHDNhap
                              select p).Single();

             tamObject.MaDonDatNCC = nhapObject.MaDonDatNCC;

             tamObject.NgayMua = nhapObject.NgayMua;

             tamObject.ThueSuat = nhapObject.ThueSuat;
             tamObject.TongGT = nhapObject.TongGT;

             tamObject.TienSauThue = nhapObject.TienSauThue;


             foreach (var chiTietTamObject in tamObject.ChiTietHDNhaps)
             {
                 if (!nhapObject.TonTaiChiTietHDMua(chiTietTamObject.MaCTHDN))
                     db.ChiTietHDNhaps.DeleteOnSubmit(chiTietTamObject);
             }


             foreach (var chiTietHDMua in nhapObject.DanhSachChiTietHDMua)
             {
                 if (chiTietHDMua.MaCTHDN == 0)
                 {
                     DataAccess.ChiTietHDNhap chiTietTamObject = new DataAccess.ChiTietHDNhap();
                     chiTietTamObject.MaHDNhap = tamObject.MaHDNhap;

                     chiTietTamObject.MaHang = chiTietHDMua.MaHang;
                     chiTietTamObject.SoLuong = chiTietHDMua.SoLuong;

                     chiTietTamObject.GiaNhap = chiTietHDMua.GiaNhap;
                     chiTietTamObject.ChietKhau = chiTietHDMua.ChietKhau;

                     db.ChiTietHDNhaps.InsertOnSubmit(chiTietTamObject);
                 }
                 else
                 {
                     var chiTietTamObject = (from p in db.ChiTietHDNhaps
                                             where p.MaCTHDN == chiTietHDMua.MaCTHDN
                                             select p).Single();

                     chiTietTamObject.MaHDNhap = tamObject.MaHDNhap;

                     chiTietTamObject.MaHang = chiTietHDMua.MaHang;
                     chiTietTamObject.SoLuong = chiTietHDMua.SoLuong;
                     chiTietTamObject.GiaNhap = chiTietHDMua.GiaNhap;
                     chiTietTamObject.ChietKhau = chiTietHDMua.ChietKhau;
                 }
             }

             db.SubmitChanges();
         }


         public static void Xoa(int maHDMua)
         {
             DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

             var xoaObject = (from p in db.HoaDonNhaps
                              where p.MaHDNhap == maHDMua
                              select p).Single();
             db.HoaDonNhaps.DeleteOnSubmit(xoaObject);

             db.SubmitChanges();
         }

         public static HoaDonMua LayHoaDonMua(int maHDMua)
         {
             DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
             HoaDonMua hoaDonMua = (from p in db.HoaDonNhaps
                                    where p.MaHDNhap == maHDMua
                                    select new HoaDonMua(p.MaHDNhap,p.MaDonDatNCC,p.NgayMua,p.TongGT,p.TienSauThue,p.ThueSuat)).Single();

             List<ChiTietHDMua> danhsachChiTietDonDat = (from p in db.ChiTietHDNhaps
                                                         where p.MaHDNhap == maHDMua
                                                         select new ChiTietHDMua(p.MaCTHDN,p.MaHDNhap,p.MaHang,p.HangHoa.TenHang,p.GiaNhap,p.SoLuong,p.ChietKhau)).ToList();

             if (danhsachChiTietDonDat == null || danhsachChiTietDonDat.Count == 0)
                 danhsachChiTietDonDat = new List<ChiTietHDMua>();

             hoaDonMua.DanhSachChiTietHDMua=  danhsachChiTietDonDat;

             return hoaDonMua;
         }

         public static List<HoaDonMua> LayDanhSach()
         {
             DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
             return (from p in db.HoaDonNhaps
                     select new HoaDonMua(p.MaHDNhap,p.MaDonDatNCC,p.NgayMua,p.TongGT,p.TienSauThue,p.ThueSuat)).ToList();
         }

         public static List<HoaDonMua> LayDanhSach_NgayMua(DateTime tungay, DateTime denngay)
         {
             try
             {
                 DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
                 return (from p in db.HoaDonNhaps
                         where p.NgayMua.Date >= tungay.Date && p.NgayMua.Date <= denngay.Date
                         select new HoaDonMua(p.MaHDNhap,p.MaDonDatNCC,p.NgayMua,p.TongGT,p.TienSauThue,p.ThueSuat)).ToList();

             }
             catch
             {
                 return LayDanhSach();
             }
         }

         public static List<HoaDonMua> LayDanhSach_MaDonDat(string maDDHT)
         {

             try
             {

                 int maDDH = Int16.Parse(maDDHT.Substring(5));
                 DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

                 return (from p in db.HoaDonNhaps
                         where p.MaDonDatNCC == maDDH

                         select new HoaDonMua(p.MaHDNhap,p.MaDonDatNCC,p.NgayMua,p.TongGT,p.TienSauThue,p.ThueSuat)).ToList();

             }
             catch
             {
                 return LayDanhSach();
             }
         }

         public static List<HoaDonMua> LayDanhSach_MaHoaDonMua(string maHDMuaHienThi)
         {
             try
             {
                 int maHDM = Int16.Parse(maHDMuaHienThi.Substring(3));
                 DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
                 return (from p in db.HoaDonNhaps

                         where p.MaHDNhap == maHDM
                         select new HoaDonMua(p.MaHDNhap,p.MaDonDatNCC,p.NgayMua,p.TongGT,p.TienSauThue,p.ThueSuat)).ToList();
             }
             catch
             {
                 return LayDanhSach();
             }
         }


    }
}
